package com.example.mydataapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.room.Database;

import java.util.List;

public class Room extends Fragment {
    private TextView textView;
    public Room(){
        super(R.layout.sqljiemian);
    }
  /*  private TextView con;
    private  String username;
    private int age;
    private EditText Name;
    private EditText Age;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Name = getView().findViewById(R.id.textView2);
        Age=getView().findViewById(R.id.textView4);
        Button Add=getView().findViewById(R.id.button7);
        Add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username=Name.getText().toString();
                age=Integer.parseInt(Age.getText().toString());
                MyRoomDatabase db=MyRoomDatabase.getInstance(getContext());
                User user=new User();
                user.username=username;
                user.age=age;
                db.roomDao().insert(user);
            }
        });
        Button AgeList=getView().findViewById(R.id.button8);
        AgeList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyRoomDatabase db=MyRoomDatabase.getInstance(getContext());
                showUsers(db.roomDao().getAllByAge(18));
            }
        });
        MyRoomDatabase db=MyRoomDatabase.getInstance(getContext());
        showUsers(db.roomDao().getAll());
    }
*/

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            textView = getView().findViewById(R.id.textView6);
            EditText e1=getView().findViewById(R.id.textView2);
            EditText e2=getView().findViewById(R.id.textView4);
            MyRoomDatabase db;
            db = MyRoomDatabase.getInstance(getContext());
            getView()
                    .findViewById(R.id.button7)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            User user=new User();
                            user.username=e1.getText().toString();
                            int age=Integer.parseInt(e2.getText().toString());
                            user.age=age;
                            db.roomDao().insert(user);
                            showUsers(db.roomDao().getAll());
                        }
                    });
            getView()
                    .findViewById(R.id.button8)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            showUsers(db.roomDao().getAllByAge(18));
                        }
                    });

        }
    private void showUsers(List<User> users){
     //   con=getView().findViewById(R.id.textView6);
        StringBuilder stringBuilder=new StringBuilder();
        for (User user:users){
            stringBuilder.append("id:")
                    .append(user.id)
                    .append("   username")
                    .append(user.username)
                    .append("   age")
                    .append(user.age)
                    .append("\n");
        }
        textView.setText(stringBuilder.toString());
    }

}
